<?php
class StudyinterestsController extends AppController {

var $name = 'Studyinterests';

var $uses = array('Studyinterest','Application');

function beforeFilter() {
	parent::beforeFilter();
	if (isset($this->passedArgs[0]) && $this->Auth->user('group_id') == 2) {
  		$aco = $this->Acl->Aco->findByModelAndForeignKey('Studyinterest', $this->passedArgs[0]);
    	$aro = $this->Acl->Aro->findByModelAndForeignKey('User', $this->Auth->user('id'));
    	if (!$this->Acl->check($aro['Aro'], $aco['Aco'])) {
      	$this->Session->setFlash($this->Auth->authError, 'flash_failure');
        $this->redirect($this->referer());
    	}
		}
}

///////////////////////////////////////////////////////////////////////////////	

function edit($id = null) {
	if (!$id && empty($this->data)) {
		$this->Session->setFlash(__('Invalid Studyinterest', true), 'flash_failure');
		$this->redirect($this->referer());
	}
	
	if (!empty($this->data)) {
		if ($this->Studyinterest->save($this->data)) {
			$app_id = $this->Studyinterest->field('application_id');
			//We saved so change status.
			$this->Application->change_status($app_id, 'studyinterest', 2, '');
			$this->Session->setFlash(__('Your Research Interests have been updated', true), 'flash_success');
		} else {
			$this->Session->setFlash(
				__('Your Research interests could not be saved. Please, try again.', true), 'flash_failure');
		}
	}
	if (empty($this->data)) {
		$this->data = $this->Studyinterest->read(null, $id);
	}	
	
	$this->set('app_id', $this->Studyinterest->field('application_id'));	
	//Now set some values from the appInfoElements.
	$this->set('grad_area', $this->_parseOptions('studyinterest_grad_area')); 
	$this->set('res_area', $this->_parseOptions('studyinterest_research_area'));
	$this->set('faculty_list', $this->_parseOptions('studyinterest_faculty_list'));
}

} //end controller.
